package com.example.administrator.zhongyichou.wxapi;

/**
 * Created by lichenyu on 2017/12/6.
 * 描述：
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.example.administrator.zhongyichou.R;
import com.example.administrator.zhongyichou.config.Constants;
import com.ips.ips2wxassist.assist.IPS2WXAssist;
import com.ips.ips2wxassist.entity.WXRequest;
import com.ips.ips2wxassist.entity.WXResponse;
import com.ips.ips2wxassist.listener.ResponseEventHandler;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.mm.opensdk.utils.Log;

/**
 * -Created by Luke on 2017/2/14.
 */
public class WXPayEntryActivity extends AppCompatActivity implements ResponseEventHandler {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_results);

        WXRequest request = new WXRequest();
        request.setAppId(Constants.APP_ID);
        new IPS2WXAssist(WXPayEntryActivity.this).registerEventHandler(request, this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    public void onResponse(WXResponse response) {
        //response.getErrCode == -1 原因：支付错误, 可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
        //response.getErrCode == -2 原因 用户取消,无需处理。发生场景：用户不支付了，点击取消，返回APP
        switch (response.getErrorCode()) {
            case 0:
                Toast.makeText(WXPayEntryActivity.this, "操作成功" + "errorCode = " + response.getErrorCode(), Toast.LENGTH_SHORT).show();
                break;
            case -1:
                Toast.makeText(WXPayEntryActivity.this, "支付错误" + "errorCode = " + response.getErrorCode(), Toast.LENGTH_SHORT).show();
                finish();
                break;
            case -2:
                Toast.makeText(WXPayEntryActivity.this, "用户取消" + "errorCode = " + response.getErrorCode(), Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                break;
        }
    }
}